import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class makes reference to the shield for the player.
 * 
 * @author (Arturo Aaron Rios Obregon) 
 * @version (25 june 2013)
 */
public class Escudo extends Bala
{
    private int vida;
    private SimpleTimer cTimer;
    
    /**
     * Contructor
     */
    public Escudo()
    {
        super(0);
        vida=30;
        cTimer=new SimpleTimer();
    }
    
    /**
     * move and remove the shield
     */
    public void act() 
    {
        mueve();
        tocaBala();
        borraEscudo();
        // Add your action code here.
    }
    
    /**
     *  remove the shield if his life is zero or after certain time.
     */
    public void borraEscudo()
    {
        if(vida==0||cTimer.millisElapsed()/1000>3)
            getWorld().removeObject(this);
    }
    
    /**
     * If the shield touch a bullet reduces his life.
     */
    public void tocaBala()
    {
        if(getOneIntersectingObject(BulletEnemy.class)!=null)
        {
            vida=vida-10;
            getWorld().removeObject(getOneIntersectingObject(BulletEnemy.class));
        }
    }
    
    /**
     * move the shield
     */
    public void mueve()
    {
        int x = super.getX();
        int y = super.getY();
        if(Greenfoot.isKeyDown("up"))
            if(y>0)
                setLocation(x,y-7);
        if(Greenfoot.isKeyDown("down"))        
            if(y<getWorld().getHeight())
                setLocation(x,y+7);
        if(Greenfoot.isKeyDown("left"))
            if(x>0)
                setLocation(x-7,y);        
        if(Greenfoot.isKeyDown("right"))        
            if(x<getWorld().getWidth())
                setLocation(x+7,y);
    }
}
